package com.cn.dim.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cn.dim.pojo.Chkinputitem;
import com.cn.dim.pojo.Pages;
import com.cn.dim.service.ChkinputitemService;
import com.github.pagehelper.Page;


@Controller
@RequestMapping("/chkinputitem")
public class ChkinputitemController {

	@Resource
	private ChkinputitemService chkinputitemService;
	@RequestMapping(value ="/queryChkinputitem",method = RequestMethod.GET)
	@ResponseBody
	//Pages是自己定义的页面实体�?
	public Map<String,Object> queryChkinputitem(HttpServletRequest request,Pages pages,String chkpointid){
		Map<String,Object> param=new HashMap<>();//定义查询参数
		Map<String,Object> reMap=new HashMap<>();//定义返回值，其中有一项必须是集合，先按照下面的写法
		//参数p是列表页中以查询条件数据临时组成的过渡chkinputitem对象
		param.put("pageIndex", pages.getPage());
		param.put("pageSize", pages.getRows());
		param.put("chkpointid", chkpointid);//查询条件是巡查点id
		
		try {
			Page<Map<String,Object>> rList = (Page)chkinputitemService.queryChkinputitem(param);
			//Page对象认为是一个以键值对保存的一行记录的多条记录的集合，
			System.out.println(rList);
			reMap.put("rows", rList);
			reMap.put("total", rList.getTotal());
			reMap.put("status", "success");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			reMap.put("status", "error");
		}
		return reMap;
	}
	
	@RequestMapping(value ="/insertChkinputitem",method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> insertChkinputitem(Chkinputitem cii,String chkpointid)   
	{
		Map<String,Object> reMap=new HashMap<>();//返回值map
		String chkinputitemid=com.cn.dim.utils.StringUtil.makeUUID();
		cii.setId(chkinputitemid);
		cii.setChkpointid(chkpointid);
		cii.setIsdel("0");
		
		try 
		{
			int flag = chkinputitemService.insertChkinputitem(cii);
			if(flag>0)
			{
				reMap.put("message", "插入成功");
				reMap.put("status", "success");
			}
			else
			{
				reMap.put("status", "error");
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			reMap.put("status", "error");
		}
		return reMap;
	}
	
	@RequestMapping(value ="/updateChkinputitem",method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> updateChkinputitem(Chkinputitem cii,String chkinputitemid)  
	{
		Map<String,Object> reMap=new HashMap<>();//返回值map
		cii.setId(chkinputitemid);//实体对象传来的并不含有id的值，这里要使用页面传过来chkinputitemid值进行填充
		try 
		{
			int flag = chkinputitemService.updateChkinputitem(cii);
			if(flag>0)
			{
				reMap.put("message", "修改成功");
				reMap.put("status", "success");
			}
			else
			{
				reMap.put("status", "error");
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			reMap.put("status", "error");
		}
		return reMap;
	}

	@RequestMapping(value ="/delChkinputitem",method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> delChkinputitem(HttpServletRequest request,String chkinputitemid) 
	{
		Map<String,Object> reMap=new HashMap<>();//返回值map
        Map<String,Object> m=new HashMap<>();//构造service接口中delChkinputitem方法的参数
        m.put("chkinputitemid", chkinputitemid);
		try 
		{
			int flag = chkinputitemService.delChkinputitem(m);
			if(flag>0)
			{
				reMap.put("message", "删除成功");
				reMap.put("status", "success");
			}
			else
			{
				reMap.put("status", "error");
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			reMap.put("status", "error");
		}
		return reMap;
	}
}
